﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for ParkingFloorDAL
/// </summary>
public class ParkingFloorDAL
{
	public ParkingFloorDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static List<ParkingFloor> GetAll()
    {
        string strSQL = @"SELECT [ParkingFloorID]
                          ,[Name]
                          ,[Description]
                          ,P.[LastUser]
                          ,P.[LastUpdate]
                          ,E.FirstName
                          ,E.LastName
                      FROM 
                        [ParkingFloor] P
                      LEFT JOIN   
                        Employee E ON E.EmployeeID=P.LastUser
                        
                        ORDER BY Name";
        SqlDataReader reader;
        List<ParkingFloor> list =new List<ParkingFloor>();
        ParkingFloor floor = null;
        try
        {
            reader = SqlHelper.ExecuteReader(CommandType.Text, strSQL);
            if (reader.HasRows)
            {
                list = new List<ParkingFloor>();
                while (reader.Read())
                {
                    floor = new ParkingFloor();
                    floor.ID = (Guid)reader["ParkingFloorID"];
                    floor.Name = reader["Name"].ToString();
                    floor.Description = reader["Description"].ToString();

                    Employee emp = new Employee();
                    emp.Id = SqlHelper.CheckGuidNull(reader["LastUser"]);
                    emp.FirstName = SqlHelper.CheckStringNull(reader["FirstName"]);
                    emp.LastName = SqlHelper.CheckStringNull(reader["LastName"]);
                    floor.LastUser = emp;
                    list.Add(floor);
                }
            }
            return list;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public static string GetName(Guid id)
    {
        string strSQL = @"SELECT [Name]      
                          FROM [ParkingFloor]
                          WHERE [ParkingFloorID]=@ParkingFloorID";

        //parms
        SqlParameter parm = new SqlParameter("@ParkingFloorID", id);

        try
        {
            object obj = SqlHelper.ExecuteScalar(CommandType.Text, strSQL, parm);
            if (obj == null) return String.Empty;
            return obj.ToString();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public static bool Delete(Guid guid)
    {
        string strSQL = "DELETE FROM [ParkingFloor] WHERE [ParkingFloorID]=@ParkingFloorID";

        //parms
        SqlParameter parm = new SqlParameter("@ParkingFloorID", guid);
        try
        {
            return (SqlHelper.ExecuteNonQuery(CommandType.Text, strSQL, parm) > 0);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public static bool IsExistCardType(string p)
    {
        #region Sql sentence
        string strSQL = @"Select count(ParkingFloorID) from [ParkingFloor] WHERE Name=@Name";

        #endregion

        //parms
        SqlParameter[] parms = new SqlParameter[]{
                        new SqlParameter("@Name",p)
                    };
        try
        {
            return Int16.Parse(SqlHelper.ExecuteScalar(CommandType.Text, strSQL, parms).ToString()) > 0;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public static bool InsertOrUpdate(ParkingFloor parking)
    {

        #region Sql sentence
        string strSQLInsert = @"INSERT INTO [ParkingFloor] (
	                                    [ParkingFloorID],
	                                    [Name],
	                                    [Description],
	                                    [LastUser],
	                                    [LastUpdate]
                                    ) VALUES (
	                                    NewID(),
	                                    @Name,
	                                    @Description,
	                                    @LastUser,
	                                    GetDate()
                                    )";

        string strSQLUpdate = @"UPDATE [ParkingFloor] SET
	                                    [Name] = @Name,
	                                    [Description] = @Description,
	                                    [LastUser] = @LastUser,
	                                    [LastUpdate] = GetDate()
                                    WHERE
	                                    [ParkingFloorID] = @ParkingFloorID";
        #endregion

        //parms
        SqlParameter[] parms = new SqlParameter[]{
                        new SqlParameter("@ParkingFloorID",parking.ID),
                        new SqlParameter("@Name",parking.Name),
                        new SqlParameter("@Description",parking.Description),
                        new SqlParameter("@LastUser",parking.LastUser.Id),
                    };

        string strSQL;
        //Insert case
        if (parking.ID == Guid.Empty)
        {
            strSQL = strSQLInsert;
        }
        //delete case
        else
        {
            strSQL = strSQLUpdate;
        }

        try
        {
            return (SqlHelper.ExecuteNonQuery(CommandType.Text, strSQL, parms) > 0);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public static ParkingFloor GetById(Guid guid)
    {
        string strSQL = @"SELECT
	                            [ParkingFloorID],
	                            [Name],
	                            [Description]
                            FROM
	                            [ParkingFloor]
                            WHERE
	                            [ParkingFloorID] = @ParkingFloorID";
        SqlDataReader reader;
        ParkingFloor parkingFloor = null;

        //parms
        SqlParameter parm = new SqlParameter("@ParkingFloorID", guid);


        try
        {
            reader = SqlHelper.ExecuteReader(CommandType.Text, strSQL, parm);

            if (reader.HasRows)
            {
                reader.Read();
                parkingFloor = new ParkingFloor();
                parkingFloor.ID = (Guid)reader["ParkingFloorID"];
                parkingFloor.Name = reader["Name"].ToString();
                parkingFloor.Description = reader["Description"].ToString();
            }
            return parkingFloor;
        }
        catch (Exception)
        {
            throw;
        }
    }
}